package com.yiyi.credit.manage.sso.service.inner;

import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.mapper.SysUserInfoMapper;
import com.yiyi.credit.manage.sso.mapper.SysUserRoleMapper;
import com.yiyi.credit.manage.sso.model.entity.SysUserInfo;
import com.yiyi.credit.manage.sso.model.entity.SysUserRole;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class SysUserInfoServiceImpl implements SysUserInfoService {

    @Autowired
    SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;



    @Override
    public SysUserInfo getSysUserInfoById(SysUserInfo sysUserInfo) throws ServiceException {
        return sysUserInfoMapper.selectOne(sysUserInfo);
    }


    @Override
    public int registerSysUserInfo(SysUserInfo sysUserInfo) throws ServiceException {

        //判断名称是否存在
        sysUserInfo.setCreateTime(new Date().getTime());
        sysUserInfo.setUpdateTime(new Date().getTime());
        return sysUserInfoMapper.insertSelective(sysUserInfo);
    }


    @Override
    public int updateSysUserInfo(SysUserInfo sysUserInfo) throws ServiceException {

        //判断名称是否存在
        SysUserInfo checkSysUserInfo = getSysUserById(sysUserInfo.getId());
        if (checkSysUserInfo == null) {
            throw new ServiceException(ResponseCode.USER_NOT_EXIST);
        }

        Example example = new Example(SysUserRole.class);
        example.createCriteria().andEqualTo("userId", sysUserInfo.getUid());
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectByExample(example);

        if (sysUserRoleList.size() <= 0) {
            throw new ServiceException(ResponseCode.USER_NO_ROLE);
        }


        List<Map<String, Object>> checkMap = sysUserInfoMapper.checkPhone(sysUserInfo.getPhone(), sysUserRoleList.get(0).getRoleId());

        if (sysUserInfo.getId() == null) {
            if (checkMap.size() > 0) {
                throw new ServiceException(ResponseCode.EXIST_PHONE);
            }
        }
        if (sysUserInfo.getId() != null) {
            if (checkMap.size() > 1) {
                throw new ServiceException(ResponseCode.EXIST_PHONE);
            }
        }

        // List<SysUserInfo> sysUserInfoList = getSysUserInfoByPhone(sysUserInfo.getPhone());
        //
        // if (sysUserInfoList.size() > 0) {
        //     throw new ServiceException(ResponseCode.EXIST_PHONE);
        // }

        sysUserInfo.setUpdateTime(new Date().getTime());
        return sysUserInfoMapper.updateByPrimaryKeySelective(sysUserInfo);
    }


    @Override
    public int deleteSysUserInfo(SysUserInfo sysUserInfo) throws ServiceException {

        //判断名称是否存在
        List<SysUserInfo> list = getSysUserInfoByPhone(sysUserInfo.getPhone());
        if (list == null || list.size() == Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.USER_NOT_EXIST);
        }
        return sysUserInfoMapper.delete(sysUserInfo);
    }


    @Override
    public SysUserInfo getSysUserById(Long id) throws ServiceException {

        return sysUserInfoMapper.selectByPrimaryKey(id);
    }


    public List<SysUserInfo> getSysUserBySid(Long uid) throws ServiceException {
        Example example = new Example(SysUserInfo.class);
        example.createCriteria().andEqualTo("uid", uid);
        return sysUserInfoMapper.selectByExample(example);
    }


    @Override
    public List<SysUserInfo> getSysUserInfoByPhone(String phone) throws ServiceException {

        Example example = new Example(SysUserInfo.class);
        example.createCriteria().andEqualTo("phone", phone);
        return sysUserInfoMapper.selectByExample(example);
    }



    @Override
    public List<SysUserInfo> getSysInfoByUserId(Long sysUserId) throws ServiceException {

        Example example = new Example(SysUserInfo.class);
        example.createCriteria().andEqualTo("uid", sysUserId);
        return sysUserInfoMapper.selectByExample(example);
    }

    @Override
    public String getEmailByUsername(String username) throws ServiceException {
        if (StringUtils.isBlank(username)) {
            throw new ServiceException("用户名不能为空");
        }
        return sysUserInfoMapper.getEmailByUsername(username);
    }
}
